package com.lutzyt.delivery.service.impl;

import com.lutzyt.delivery.bean.*;
import com.lutzyt.delivery.bean.req.Comment;
import com.lutzyt.delivery.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zyt
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {

  private CommodityCommentService commodityCommentService;
  private HorsemanCommentService horsemanCommentService;
  private OrderCommentService orderCommentService;
  private OrderService orderService;
  private CustomerService customerService;

  @Override
  public void comment(Comment comment) {
    final Customer customer = customerService.findById(comment.getOrderComment().getCustomerId());
    log.info("评论{}", comment);
    final OrderComment orderComment = comment.getOrderComment();
    final List<CommodityComment> commodityCommentList = comment.getCommodityCommentList();
    final HorsemanComment horsemanComment = comment.getHorsemanComment();
    orderComment.setCustomerName(customer.getName());
    orderComment.setState("INIT");
    orderCommentService.insert(orderComment);
    horsemanComment.setCustomerName(customer.getName());
    horsemanComment.setState("INIT");
    horsemanCommentService.insert(horsemanComment);
    for (CommodityComment commodityComment : commodityCommentList) {
      commodityComment.setCustomerName(customer.getName());
      commodityComment.setState("INIT");
      commodityCommentService.insert(commodityComment);
    }
    final Integer orderId = orderComment.getOrderId();
    final Order order = orderService.getById(orderId);
    if (!"COMMENT".equals(order.getState())){
      throw new RuntimeException("该订单状态以更改");
    }
    order.setState("FINISH");
    orderService.updateById(order);
  }

  @Autowired
  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  @Autowired
  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }

  @Autowired
  public void setHorsemanCommentService(HorsemanCommentService horsemanCommentService) {
    this.horsemanCommentService = horsemanCommentService;
  }

  @Autowired
  public void setCommodityCommentService(CommodityCommentService commodityCommentService) {
    this.commodityCommentService = commodityCommentService;
  }

  @Autowired
  public void setOrderCommentService(OrderCommentService orderCommentService) {
    this.orderCommentService = orderCommentService;
  }
}
